﻿using Microsoft.Extensions.Configuration;
using System;
using System.IO;

namespace ShopMall.Common
{
    public static class ConfigurationHelper
    {
        private static bool useRedis;
        private static int expireMinutes;

        public static int ExpireMinutes { get => expireMinutes; set => expireMinutes = value; }
        public static bool UseCache { get => useRedis; set => useRedis = value; }


        /// <summary>
        ///  读取json文件
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="ParentKey">父级json节点</param>
        /// <param name="childrenKey">子级节点</param>
        /// <param name="path">appsettings.json</param>
        /// <returns></returns>
        public static object GetAppSettings<T>(string parentKey, string childrenKey = null, int pathType = 2, string path = "appSettings.json")
        {
            object strInfo = string.Empty;
            IConfigurationBuilder builder = null;
            if(pathType==1)
                builder=new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile(path,false,true);
            else if (pathType == 2)
                builder = new ConfigurationBuilder().SetBasePath(AppDomain.CurrentDomain.BaseDirectory).AddJsonFile(path, false, true);
            //.Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true });

            var configuration = builder.Build();
            try
            {
                if (!string.IsNullOrWhiteSpace(childrenKey))
                {
                    strInfo = configuration.GetSection(parentKey)[childrenKey].ToString();
                }
                else
                {
                    strInfo = configuration[parentKey].ToString();
                }
                return strInfo;
            }
            catch (Exception ex)
            {
                //LogHelper.Error(ex.Message.ToString());
                //Log2.WriteLog(ex.Message.ToString());
                return null;
            }
        }

        //string userName = Configuration.GetSection("AppConfiguration")["UserName"];
    }
}
